﻿using System;
using System.Collections.Generic;
using System.Text;

using System.IO;

using RegistryHelper;
using LoggingServer;

namespace LoggingServerInstall
{
    public class Install
    {
        public bool InstallFolder()
        {
            try
            {
                if (!Directory.Exists(StaticComponent.Path))
                {
                    Directory.CreateDirectory(StaticComponent.Path);
                }

                if (!Directory.Exists(StaticComponent.PathLog))
                {
                    Directory.CreateDirectory(StaticComponent.PathLog);
                }


                return true;
            }
            catch (Exception ex)
            {
                StaticComponent.LogEx(ex);
                return false;
            }
        }


        public bool Start()
        {
            if (InstallFolder())
            {
                Console.WriteLine("");
                Console.WriteLine("******************* Folder Installed *******************");
                Console.WriteLine("");
            }
            else
            {
                return false;
            }

            if (InstallReg())
            {
                Console.WriteLine("");
                Console.WriteLine("******************* Reg Installed *******************");
                Console.WriteLine("");
            }
            else
            {
                return false;
            }

            return true;
        }


        RegistryUtils register = new RegistryUtils(RootKey.HKEY_LOCAL_MACHINE);

        public static readonly string Logging_Path = @"SOFTWARE\GAME_SERVER\Logging";
        public static readonly string Console_Path = @"SOFTWARE\GAME_SERVER\Console";

        public static readonly string LogServerException = "1";
        public static readonly string ConsoleVisiable = "1";

        public bool InstallReg()
        {
            try
            {
                bool going = false;

                going = InstallRoot(Logging_Path);

                if (going)
                {
                    going = InstallRoot(Console_Path);
                }
                else
                {
                    return false;
                }

                if (going)
                {
                    going = InstallKey(Logging_Path, "LogServerException", LogServerException);
                }
                else
                {
                    return false;
                }

                if (going)
                {
                    going = InstallKey(Console_Path, "ConsoleVisiable", ConsoleVisiable);
                }
                else
                {
                    return false;
                }

                if (going)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                StaticComponent.LogEx(ex);
                return false;
            }

        }

        public bool InstallRoot(string _Logging_Path)
        {
            try
            {
                register.CreateRegKey(_Logging_Path);

                Console.WriteLine(string.Format("[{0}] is created", _Logging_Path));

                return true;
            }
            catch (Exception ex)
            {
                StaticComponent.LogEx(ex);
                return false;
            }
        }

        public bool InstallKey(string path, string keyName, string value)
        {
            try
            {
                register.SetRegVal(path, keyName, value);
                Console.WriteLine(string.Format(@"[{0}\{1}] = [{2}] is created", path, keyName, value));
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }


    }
}
